package nickthissen.iracing.common;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;

import nickthissen.iracing.datamodels.PostList;
import nickthissen.iracing.datamodels.PrivateMessage;
import nickthissen.iracing.datamodels.PrivateMessageItem;
import nickthissen.iracing.datamodels.RecentThread;
import nickthissen.iracing.datamodels.ThreadList;

/**
 * Created by Nick on 8/10/13.
 */
public class Cache
{
    private static boolean isInitialized;

    private static HashMap<String, ThreadList> forums;
    private static HashMap<String, PostList> threads;
    private static HashMap<Integer, ArrayList<RecentThread>> recentPopularThreads;
    private static ArrayList<PrivateMessageItem> pms;

    private static void init()
    {
        if (!isInitialized)
        {
            forums = new HashMap<String, ThreadList>();
            threads = new HashMap<String, PostList>();
            recentPopularThreads = new HashMap<Integer, ArrayList<RecentThread>>();
            pms = new ArrayList<PrivateMessageItem>();

            isInitialized = true;
        }
    }

    public static void savePms(ArrayList<PrivateMessageItem> items)
    {
        init();
        pms.clear();
        pms.addAll(items);
    }

    public static ArrayList<PrivateMessageItem> loadPms()
    {
        init();
        if (pms.size() > 0) return pms;
        return null;
    }

    public static void saveRecentPopular(ArrayList<RecentThread> items, int type)
    {
        init();
        if (recentPopularThreads.containsKey(type)) recentPopularThreads.remove(type);
        recentPopularThreads.put(type, items);
    }

    public static ArrayList<RecentThread> loadRecentPopular(int type)
    {
        init();
        if (recentPopularThreads.containsKey(type)) return recentPopularThreads.get(type);
        return null;
    }

    public static void saveForum(ThreadList threadlist, String url)
    {
        init();
        removeForum(url);
        forums.put(url, threadlist);
    }

    public static void removeForum(String url)
    {
        init();
        if (forums.containsKey(url)) forums.remove(url);
    }

    public static ThreadList loadForum(String url)
    {
        init();
        if (forums.containsKey(url)) return forums.get(url);
        return null;
    }

    public static void saveThread(PostList postlist, String url)
    {
        init();
        removeThread(url);
        threads.put(url, postlist);
    }

    public static PostList loadThread(String url)
    {
        init();
        if (threads.containsKey(url)) return threads.get(url);
        return null;
    }

    public static void removeThread(String url)
    {
        init();
        if (threads.containsKey(url)) threads.remove(url);
    }
}
